package com.project.controller.web;

import com.project.entity.Class;
import com.project.myBeans.ResponseBean;
import com.project.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: WheelChen
 * data: 2017/7/13
 * function: 班级相关操作
 */
@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @RequestMapping(value = "/findBatchByMajorid/{majorid}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 根据专业批量查找班级
     *
     * @param majorid
     * @return
     */
    public ResponseBean findBatchByMajorid(@PathVariable("majorid") Integer majorid){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<Class> classes = classService.findBatchByMajorid(majorid);
            responseBean.setData(classes);
            responseBean.setMessage("获取成功");
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 添加单个班级
     *
     * @param clazz
     * @return
     */
    public ResponseBean addOne(@RequestBody Class clazz){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (classService.checkClassIsExist(clazz)){
                responseBean.setSuccess(false);
                responseBean.setMessage("班级已存在");
            }
            else {
                classService.add(clazz);
                responseBean.setSuccess(true);
                responseBean.setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/checkMajorIsExist", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查班级是否已存在
     *
     * @param clazz
     * @return
     */
    public ResponseBean checkMajorIsExist(@RequestBody Class clazz){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (classService.checkClassIsExist(clazz)){
                responseBean.setSuccess(false);
                responseBean.setMessage("班级已存在");
            }
            else {
                responseBean.setSuccess(true);
                responseBean.setMessage("不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }
}
